<?php

namespace Biz\model\theParagraph;

class CollocationImg extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_the_paragraph';

    const _DATABASE_ = 'the_paragraph';

    const _TABLE_NAME = 'collocation_img';


    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;


    public static function addCollocationImg($storeId, $roomId, $collocationId, $imgIds)
    {
        foreach ($imgIds as $imgId) {
            $data = [
                'store_id' => $storeId,
                'room_id' => $roomId,
                'collocation_id' => $collocationId,
                'pic_id' => $imgId,
                'status' => self::STATUS_NORMAL,
                'addtime' => date('Y-m-d H:i:s')
            ];
            self::insert($data);
        }
        return true;
    }

    public static function saveCollocationImgSort($sortInfo)
    {
        foreach ($sortInfo as $imgId => $sort) {
            self::update(['sort' => $sort], ['id' => $imgId]);
        }
    }


    public static function getImgByCollocationId($collocationId)
    {
        $imgList = self::select("*",
            [
                'AND' => [
                    'collocation_id' => $collocationId,
                    'status' => self::STATUS_NORMAL
                ],
                'ORDER' => [
                    'sort',
                    'id'
                ]
            ]);
        return $imgList;
    }

    public static function changeImgStatusById($id, $status)
    {
        if (self::update(['status' => $status], ["id" => $id])->rowcount()) {
            return $id;
        }
    }
}